package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.LoginInfo
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.IBindPoolView
import de.baumann.browser.model.UserModel
import de.baumann.browser.utils.*

class BindPoolPresenter : BasePresenter<IBindPoolView>() {
    private val userModel: UserModel = UserModel()

    fun inviteCode() {
        userModel.inviteCode(getMoneyAddress(), view.getInvitation())
                .subscribe(object : OdinObserver<String>(view) {
                    override fun onSuccess(str: String) {
                        login()
                    }
                })
    }

    fun login() {
        userModel.login(getMoneyAddress(), "")
                .subscribe(object : OdinObserver<LoginInfo>(view) {
                    override fun onSuccess(loginInfo: LoginInfo) {
                        saveUser(loginInfo)
                        UnicornUtil.setUserInfo(loginInfo)
                        saveInviteCode(loginInfo.inviteCode)
                        saveInviteCodeStatus(loginInfo.inviteId)
                        saveFormAddress(loginInfo.fromAddress)
                        view?.finishCode()
                    }
                })
    }
}